﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PictrueSimpleSyndication
{
    public delegate void CurrentIndexChange(int currentindex);

    public partial class UserControlPictrueBox : UserControl
    {
        public UserControlPictrueBox()
        {
            InitializeComponent();

            NetPictureCache.GetInstance().WatchCachePicture(PictrueCacheFinsh, this);
        }

        ~UserControlPictrueBox()
        {
            NetPictureCache.GetInstance().UnWatchCachePicture(this);
        }

        private IEnumerable<int> GetIndex(IEnumerable<ContentInfo> infos, string path)
        {
            List<int> ids = new List<int>();

            if (infos == null)
                return ids;

            for (int i = 0; i < infos.Count(); i++)
            {
                var item = infos.ElementAt(i);

                if (item.DataInfo.StartsWith("imagepath") && item.Data.ToString() == path)
                {
                    ids.Add(i);
                }
            }

            return ids;
        }

        private void PictrueCacheFinsh(object obj)
        {
            if (!(obj is NetPictureCacheInfo))
                return;

            NetPictureCacheInfo info = obj as NetPictureCacheInfo;

            if (info.CacheStatus && AllContentInfo != null)
            {
                var indexs = GetIndex(AllContentInfo, info.UriPath);
                foreach (var index in indexs)
                {
                    AllUserControlPictrues[index].SetImageLocalPath(info.LocalPath);
                }
            }

            Console.WriteLine(info.CacheStatus);
            Console.WriteLine(info.Message);
        }

        private void UserControlPictrueBox_Resize(object sender, EventArgs e)
        {
            button_last.SetPositionByAnchr(this.GetAnchrPoint(0.01, 0.5), new Anchor(0, 0.5));
            button_next.SetPositionByAnchr(this.GetAnchrPoint(0.99, 0.5), new Anchor(1, 0.5));
            panel_bottom.SetPositionByAnchr(this.GetAnchrPoint(0.5, 0.99), new Anchor(0.5, 1));

            int index = 0;
            foreach (var item in AllUserControlPictrues)
            {
                item.Size = Size;
                item.Location = new Point(Width * (index - CurrentIndex), 0);

                ++index;
            }
        }

        public int CurrentIndex { get; set; } = 0;

        public int MaxCount { get; set; }

        private void button_next_Click(object sender, EventArgs e)
        {
            if (MaxCount == 0)
                return;

            ++CurrentIndex;

            if (CurrentIndex >= MaxCount)
                CurrentIndex = 0;

            ChangeCurrentIndex();
        }

        private void button_last_Click(object sender, EventArgs e)
        {
            if (MaxCount == 0)
                return;

            --CurrentIndex;

            if (CurrentIndex < 0)
                CurrentIndex = MaxCount - 1;

            ChangeCurrentIndex();
        }


        /// <summary>
        /// 全部的图片组件
        /// </summary>
        private List<UserControlPictrues> AllUserControlPictrues { get; set; } = new List<UserControlPictrues>();

        /// <summary>
        /// 全部的包含的信息
        /// </summary>
        private IEnumerable<ContentInfo> AllContentInfo { get; set; }

        /// <summary>
        /// 初始化View
        /// </summary>
        /// <param name="AllContentInfo"></param>
        public void InitView(IEnumerable<ContentInfo> AllContentInfo)
        {
            MaxCount = AllContentInfo.Count();
            this.AllContentInfo = AllContentInfo;

            int index = 0;
            foreach (var item in AllContentInfo)
            {
                var pictrues = new UserControlPictrues();
                pictrues.Location = new Point(Width * index, 0);
                pictrues.Size = Size;

                pictrues.InitView(item);

                Controls.Add(pictrues);
                AllUserControlPictrues.Add(pictrues);

                ++index;

                pictrues.SetIndexText(index.ToString());
            }

            foreach (var item in AllContentInfo)
                NetPictureCache.GetInstance().CachePicture((string)item.Data);
        }

        private void ChangeCurrentIndex()
        {
            int index = 0;
            foreach (var item in AllUserControlPictrues)
            {
                item.Location = new Point(Width * (index - CurrentIndex), 0);
                ++index;
            }
        }
    }
}
